/*   Copyright (C) 2011 Wolfgang Hankeln, Norma J. Wendel
 *
 *   This file is part of CDinFusion.
 *
 *   CDinFusion is free software: you can redistribute it and/or
 *   modify it under the terms of the GNU Lesser General Public 
 *   License in version 3 (LGPL3) as published by the Free Software
 *   Foundation version 3 of the License.
 *
 *   CDinFusion is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *   Lesser General Public License for more details.
 *
 *   You should have received a copy of the Lesser General Public
 *   License along with CDinFusion; see the file COPYING. If not,
 *   write to the Free Software Foundation, 59 Temple Place - Suite 330,
 *   Boston, MA 02111-1307, USA or see <http://www.gnu.org/licenses/>.
 *   
 *   If you have any questions, send an email to: megx@mpi-bremen.de.
 */
package org.cdinfusion.data.studytype.generators;

public abstract class AbstractFastaHeaderGeneratorForStudyType implements
		FastaHeaderGeneratorForStudyType {

	private static final String[] MONTH_NAMES = { "", "Jan-", "Feb-", "Mar-",
			"Apr-", "May-", "Jun-", "Jul-", "Aug-", "Sept-", "Oct-", "Nov-",
			"Dec-" };

	/**
	 * Formats the date for the output in a FASTA and writes it to the given
	 * {@link StringBuilder}.
	 * 
	 * @param date
	 *            the date to be converted and written to the header.
	 * @param header
	 *            the {@link StringBuilder} the date is written to.
	 */
	protected static final void writeDate(final String date,
			final StringBuilder header) {
		// input date format: yyyy-mm-ddX..:..:..[+-]..:..

		final char[] chars = new char[date.length()];
		date.getChars(0, chars.length, chars, 0);

		if (chars.length > 7) {
			header.append(chars, 8, 2);
		}
		header.append('-');
		if (chars.length > 4) {
			header.append(MONTH_NAMES[(chars[5] & 0x0F) * 10 + chars[6] & 0x0F]);
		}
		header.append(chars, 0, 4);
	}
}
